package com.zt.springcloud.controller;

import cloud.zt.springboot.utils.ResultUtils;
import cloud.zt.springboot.utils.StringUtils;
import cloud.zt.springboot.utils.TokenUtils;
import com.zt.springcloud.entity.Address;
import com.zt.springcloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author RedReins
 * @version 1.0
 * @description: TODO
 * @date 2021/9/23 11:10
 */

@RestController
@RequestMapping("/user")
public class AddressController {
    @Autowired
    private UserService userService;

    /*
    * 添加用户地址
    * */
    @RequestMapping(value = "/addAddress",method = RequestMethod.POST)
    public ResultUtils<?> addUserAddress(Address address){
        Long userid = null;
        String username = TokenUtils.getUsername();
        if(StringUtils.isNotEmpty(username)){
            userid= userService.getLoginUser(username).getId();
        }
        System.out.println("AddressController:Address:"+address);
        address.setUserId(userid);
        return ResultUtils.success(userService.addUserAddress(address));
    }

    /*
     * 查询用户地址
     * */
    @RequestMapping(value = "/address/getUserAddresses",method = RequestMethod.GET)
    public ResultUtils<?> getAddressInfo(){
        String username = TokenUtils.getUsername();
        Long id = userService.getLoginUser(username).getId();
        return ResultUtils.success(userService.getAddressInfo(id));
    }
    /*
     * 更新用户地址
     * */
    @RequestMapping(value = "/address/modify",method = RequestMethod.POST)
    public ResultUtils<?> updateAddressInfo(Address address){
        System.out.println(address.toString());
        return ResultUtils.success(userService.updateAddressInfo(address));
    }

}
